/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package domainarchitecturesvg;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author nadaalhirabi
 */
public class DomainArchitectureSVG {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
//---------------- read the file
             // reading the file names from the user
        Scanner User_input=new Scanner(System.in);
        String file, Dom_file_path;
        
        
        System.out.print("Enter FeaturesLinkedFile file:");
        file=User_input.next();


     String domtoken = "";    
     List<String> Domtemps;
     int domlength=0;
        try (Scanner inFile = new Scanner(new File(file)).useDelimiter("\n")) {
        //try (Scanner inFile = new Scanner(new File("A_acidus_CBS_106_47_extracted.gff.txt")).useDelimiter("\n")) {

            Domtemps = new LinkedList<>();
            while (inFile.hasNext()) {
                // find next line
                domtoken = inFile.next();          
                Domtemps.add(domtoken);
                domlength++;
            }   }

    String[] tempsDomArray = Domtemps.toArray(new String[0]); 
    String[] filearray = new String[domlength];

     int b=0;
        for (String st : tempsDomArray) {//llop for all the file// ecopy to array
        filearray[b++]=st;
        }
    ////------------////     
            
    String[] DomSVGfile = new String[1];
    File dir = new File("DomainArchitectureNormalizedSVG");
    dir.mkdir();
      
    //------------------
        //for(int i=0; i<filearray.length;++i ){
                   for(int i=0; i<20;++i ){
            
            String[] parts=filearray[i].split("\t");
            String type=parts[0];//try to find gene type only
            // System.out.println(type);
            
            if(type.equals("gene")){//if it is gene we do loop to see the exons and draw them

                int normalizedGeneLength=300;
		String geneID= parts[3];
        
                //Dom chicking -----------------------------------------------
                i++;
                String[]  parts2=filearray[i].split("\t");
                String type2=parts2[0];
                float protLength; 
                float normalizedProteinLength=300;
                    
                if(type2.equals("Dom")){
                protLength= Integer.parseInt(parts2[2]);//protein length
                //Start creating the domain SVG file    
                DomSVGfile[0]="<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" height=\"30\" width=\"301\">";
                DomSVGfile[0]+="<line x1=\"0\" y1=\"25\" x2=\""+normalizedProteinLength+"\" y2=\"25\" style=\"stroke:black;stroke-width:1\" />";//creat the base line for the gene
                
                float domhight=10;
                while(type2.equals("Dom")){//start creating the domains
                    String parentGeneID= parts2[1];
                    float Domstart =(Integer.parseInt(parts2[3])/protLength)*normalizedProteinLength;  
                    float Domend = (Integer.parseInt(parts2[4])/protLength)*normalizedProteinLength;
                    float DomLength= Domend-Domstart;
                    String IntoproID=parts2[5];
                    //String IntoproDesc=parts2[6];
                    //System.out.println(Integer.parseInt(parts2[3])+ "   "+ Integer.parseInt(parts2[4]) + "   "+protLength);
                    //System.out.println(exstart+ "   "+ exend + "   "+ExonLength);

                    DomSVGfile[0]+="  <rect x=\""+ Domstart+ "\" y=\"20\" width=\""+   DomLength +   "\" height=\""  +domhight+ "\" style=\"stroke-width:2; stroke-dasharray:1,5\"/>" ;

                    
                    //++i;
                    //chick if the next line is domain & if yes >  see if ther is overlapping>> chang the hight of the second domain
                    String[] tempparts2=filearray[i+1].split("\t");
                    String temptype2=tempparts2[0];
                        if(temptype2.equals("Dom")){
                        float Domstart2 =Integer.parseInt(tempparts2[3])/protLength*normalizedProteinLength;  
                        float Domend2 = Integer.parseInt(tempparts2[4])/protLength*normalizedProteinLength;

                        //chang the hight of the overlaped domains
                            if(Domstart2<Domend){
                                if (domhight==10)domhight=8;
                                else domhight=10;
                             }//end if

                         }//end if dom testing the overlapping
                        
                        
                i++;
                parts2=filearray[i].split("\t");
                type2=parts2[0];
                    
                }//end while dom
                 
                 DomSVGfile[0]+="</svg>";
               writeTofile(DomSVGfile, "./DomainArchitectureNormalizedSVG/"+geneID+"_OverlapedDom");
                 
                }//end if dom

                //mRNA chicking -----------------------------------------------           
                 while(type2.equals("mRNA")){
                    ++i;
                    parts2=filearray[i].split("\t");
                    type2=parts2[0];  
                 }
                //exon chicking -----------------------------------------------         
                //System.out.println(type2);
                parts2=filearray[i].split("\t");
                type2=parts2[0];
                
                if(type2.equals("exon")){ 
                ++i;
                parts2=filearray[i].split("\t");
                type2=parts2[0];
                //continue;
                }//end if exon
                
                //CDS chicking -----------------------------------------------
                if(type2.equals("CDS")){ continue; }//at the end it will exit Cuz the gen is done
  
//                GeneMsvgfile[0]+="</svg>";
//                writeTofile(GeneMsvgfile, "/Users/nadaalhirabi/Desktop/Cgene_Cgenome_V2/CGenesfile/A_acidus_CBS_106_47/GeneModelSVG/A_acidus_CBS_106_47_"+geneID);
                
            }//end if gene
    }//end loop on the array
         
    }//end main
    
    
public static void writeTofile(String[] digestedfile,  String filename) throws FileNotFoundException, IOException{
    BufferedWriter writer = null;
    try {
        File file = new File(filename+".svg");  
        PrintWriter out = new PrintWriter(new FileWriter(file));  

        // Write each string in the array on a separate line  
        for ( int i=0; i<digestedfile.length ;++i) {  
            String s =digestedfile[i];
            out.println(s);  
           //if(s.equals("##END"))break;
        }  
        out.close();  
    } catch(IOException ex) {
        ex.printStackTrace();
    }  
   
}//end write to file
}
